package com.ruoyi.web.controller.api.test;

import cn.hutool.core.util.ObjectUtil;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.enums.OrderStatus;
import com.ruoyi.common.helper.LoginHelper;
import com.ruoyi.system.domain.PQuestionTopic;
import com.ruoyi.system.domain.PTestAnswer;
import com.ruoyi.system.domain.bo.*;
import com.ruoyi.system.domain.vo.PProblemTopicVo;
import com.ruoyi.system.service.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * @Description:
 * @Author: wk
 * @Date: 2022/7/29 16:43
 * @Version: 1.0
 */
@Api(value = "题目标题控制器", tags = {"前端-题目标题管理"})
@RequiredArgsConstructor
@RestController
@RequestMapping("/api/questionTopic")
public class QuestTopicApiController {

    private final IPQuestionTopicService iPQuestionTopicService;
    private final IPProblemTopicService problemTopicService;
    private final IPOrderService orderService;
    private final IPAnswerLogService answerLogService;
    private final IPTestEvaluateService evaluateService;
    private final IPTestAnswerService testAnswerService;

    @ApiOperation("查询题目标题列表")
    @GetMapping("/list")
    public TableDataInfo<PQuestionTopic> list(PQuestionTopic bo, PageQuery pageQuery) {
        return iPQuestionTopicService.queryPageMyList(bo, pageQuery);
    }

    @ApiOperation("获取题目标题详细信息")
    @GetMapping("/info/{id}")
    public R<PQuestionTopic> getInfo(@ApiParam("主键")
                                     @NotNull(message = "主键不能为空")
                                     @PathVariable("id") Long id) {
        return R.ok(iPQuestionTopicService.queryByMyId(id));
    }

    @ApiOperation("通用-查询题库列表-不分页")
    @GetMapping("/topic-com-list")
    public R<List<PProblemTopicVo>> topicComList(PProblemTopicBo bo) {
        return R.ok(problemTopicService.queryFontPageList(bo));
    }

    @ApiOperation("创建测试订单")
    @RepeatSubmit()
    @PostMapping("/add")
    public R<Void> add(@RequestBody POrderBo bo) {
        if (ObjectUtil.isEmpty(bo)) {
            return R.fail("必要参数不能为空！");
        }

        orderService.insertTestFontByBo(bo);

        return R.ok("操作成功！");
    }

    @ApiOperation("测试题答案")
    @GetMapping("/answer-info")
    public R<PTestAnswer> answerList(PTestAnswerBo bo) {
        return R.ok(testAnswerService.queryFontList(bo));
    }

    @ApiOperation("新增测试答案记录")
    @RepeatSubmit()
    @PostMapping("/add-log")
    public R<Void> addLog(@Validated(AddGroup.class) @RequestBody PAnswerLogBo bo) {
        bo.setUserId(LoginHelper.getUserId());
        answerLogService.insertByBo(bo);
        return R.ok();
    }

    @ApiOperation("新增测试题评价")
    @RepeatSubmit()
    @PostMapping("/add-evaluate")
    public R<Void> addEvaluate(@Validated(AddGroup.class) @RequestBody PTestEvaluateBo bo) {
        bo.setUserId(LoginHelper.getUserId());
        evaluateService.insertByBo(bo);
        return R.ok();
    }

    @ApiOperation("心理咨询支付成功回调")
    @PostMapping("/success-callback")
    public R<Void> successCallback(POrderBo bo) {
        if (ObjectUtil.isEmpty(bo)) {
            return R.fail("必要参数不能为空！");
        }

        if (ObjectUtil.isEmpty(bo.getId())) {
            return R.fail("必要的订单参数不能为空！");
        }

        bo.setOrderStatus(OrderStatus.WAIT_ASK.getValue());
        orderService.updateByBo(bo);
        return R.ok();
    }
}
